package rw.ac.kiosc.jdbc.school.hibernate.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import rw.ac.kiosc.jdbc.school.hibernate.SchoolSessionFactory;

public class DAO {

    protected DAO() {
    }

    public static Session getSession() {
        return SchoolSessionFactory.getSession();
    }

    protected void begin() {
        getSession().beginTransaction();
    }

    protected void commit() {
        getSession().getTransaction().commit();
    }

    protected void rollback() {
        try {
            getSession().getTransaction().rollback();
        } catch (HibernateException e) {
            System.out.println("Cannot rollback: " + e.toString());
        }
        try {
            getSession().close();
        } catch (HibernateException e) {
            System.out.println("Cannot close: " + e.toString());
        }
    }

    public static void close() {
        getSession().close();
    }
}
